import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/user/HomeView.vue'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'HomeView',
    component: HomeView,
    redirect:'/allpage',//重定向到主页,
    meta:{isTo:true},
    children:[
      {
        path:'allpage',
        name:'AllPage',
        meta:{isTo:true},
        component:()=>import('@/views/user/AllPage.vue')
      },
      {
        path:"top",
        name:'TopView',
        meta:{isTo:true},
        component:()=>import('@/views/user/TopView.vue')
      },
      {
        path:"type",
        name:'TypeView',
        meta:{isTo:true},
        component:()=>import('@/views/user/TypeView.vue')
      },
      {
        path:"cache",
        name:'CacheView',
        meta:{isTo:true},
        component:()=>import('@/views/user/CacheView.vue')
      },
      {
        path:"blog",
        name:'Blog',
        meta:{isTo:true},
        component:()=>import('@/components/Blog.vue')
      },
      {
        path:"for",
        name:'For',
        meta:{isTo:true},
        component:()=>import('@/views/user/For.vue')
      },
      
    ]   
  },
  {
    path:'/admin',
    name:'Admin',
    redirect:'/bloglist',
    meta:{isATo:true},
    component:()=>import('@/views/admin/Admin.vue'),
    children:[
      {
        path:'/bloglist',
        name:'BlogList',
        meta:{isATo:true},
        component:()=>import('@/views/admin/BlogList.vue')
      },
      {
        path:'/blogtype',
        name:'BlogType',
        meta:{isATo:true},
        component:()=>import('@/views/admin/BlogType.vue')
      },
      {
        path:'/blogtag',
        name:'BlogTag',
        meta:{isATo:true},
        component:()=>import('@/views/admin/BlogTag.vue')
      },
      {
        path:'/blogadd',
        name:'BlogAdd',
        meta:{isATo:true},
        component:()=>import('@/views/admin/BlogAdd.vue')
      },
      {
        path:'/forme',
        name:'Forme',
        meta:{isATo:true},
        component:()=>import('@/views/admin/Forme.vue')
      },
      
    ]
  },
  {
    path:'/login',
    name:'Login',
    component:()=>import('@/components/Login.vue')
  },
  {
    path:'/userlogin',
    name:'UserLogin',
    component:()=>import('@/components/UserLogin.vue')
  },
  {
    path:'/error',
    component:import('@/views/error/404.vue')
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) => {
  const user = Cookies.get('user')
  console.log(user)
  if(to.name=='Login' || to.name=='UserLogin'){
    next()
  }
  else if(to.meta.isATo){
    if(user!=null){
      next()
    }else{
      router.push('/login')
    }
  }
  else if(to.meta.isTo){
    if(user!=null){
      next()
    }else{
      router.push('/userlogin')
    }
  }
  // next()
})


export default router
